package mul.UI;

import java.util.ArrayList;
import java.util.Collections;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class AnswerPanel{

	//TODO check if the randomly genarated answers are not equal to each other
	//and maybe make them less random, i.e closer to the correct answer
	//and more
	
	private int w,h;
	private Paint bg;
	private Paint bitmapPaint;
	private int[] bitmapNumbers;
	private int len;
	private int correctAnswer;
	private int corrAnswDigits[];
	private int wr[];
	private ArrayList<Float> yPlaces;
	
	protected AnswerPanel(int w, int h) {
		this.w = w;
		this.h = h;
		bg = new Paint();
		bg.setColor(Color.BLACK);
		bg.setAlpha(200);
		bitmapPaint = new Paint();
		bitmapNumbers = new int[4];
		corrAnswDigits = new int[3];
		wr = new int[9];
		yPlaces = new ArrayList<Float>();
		yPlaces.add(h/6*2f);
		yPlaces.add(h/6*3f);
		yPlaces.add(h/6*4f);
		yPlaces.add(h/6*5f);
		
	}
	
	public void  onDraw(Canvas  canvas){
		int unit = w/100;
		//canvas.drawBitmap(MainGamePanel.background, 0, -((MainGamePanel.background.getHeight()-h)/2), null);
		//draw the multiplication
		if(len==2){
			canvas.drawBitmap(MainGamePanel.markedBitmaps[bitmapNumbers[0]], 18*1*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[11], 18*2*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.markedBitmaps[bitmapNumbers[1]], 18*3*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[12], 18*4*unit, h/6, bitmapPaint);
		}
		else{
			canvas.drawBitmap(MainGamePanel.markedBitmaps[bitmapNumbers[0]], 0, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[11], 18*1*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.markedBitmaps[bitmapNumbers[1]], 18*2*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[11], 18*3*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.markedBitmaps[bitmapNumbers[2]], 18*4*unit, h/6, bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[12], 18*5*unit, h/6, bitmapPaint);
		}

		//draw the answers
		canvas.drawBitmap(MainGamePanel.bitmaps[corrAnswDigits[0]], w/5, yPlaces.get(0), bitmapPaint);
		canvas.drawBitmap(MainGamePanel.bitmaps[wr[0]], w/5, yPlaces.get(1), bitmapPaint);
		canvas.drawBitmap(MainGamePanel.bitmaps[wr[1]], w/5, yPlaces.get(2), bitmapPaint);
		canvas.drawBitmap(MainGamePanel.bitmaps[wr[2]], w/5, yPlaces.get(3), bitmapPaint);
		if(correctAnswer>9){
			canvas.drawBitmap(MainGamePanel.bitmaps[corrAnswDigits[1]], w/5*1.75f, yPlaces.get(0), bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[wr[3]], w/5*1.75f, yPlaces.get(1), bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[wr[4]], w/5*1.75f, yPlaces.get(2), bitmapPaint);
			canvas.drawBitmap(MainGamePanel.bitmaps[wr[5]], w/5*1.75f, yPlaces.get(3), bitmapPaint);
			if(correctAnswer>99){
				canvas.drawBitmap(MainGamePanel.bitmaps[corrAnswDigits[2]], w/5*2.55f, yPlaces.get(0), bitmapPaint);
				canvas.drawBitmap(MainGamePanel.bitmaps[wr[6]], w/5*2.55f, yPlaces.get(1), bitmapPaint);
				canvas.drawBitmap(MainGamePanel.bitmaps[wr[7]], w/5*2.55f, yPlaces.get(2), bitmapPaint);
				canvas.drawBitmap(MainGamePanel.bitmaps[wr[8]], w/5*2.55f, yPlaces.get(3), bitmapPaint);
			}
		}
		
	}

	public void computeAnwser(int[] bNums){
		len = bNums.length;
		for(int i=0; i<len; i++){
			bitmapNumbers[i] = bNums[i];
		}
		correctAnswer = 1;
		for(int i=0; i<len; i++){
			correctAnswer *= bitmapNumbers[i];	
		}
		
		if(correctAnswer<10){
			corrAnswDigits[0] = correctAnswer;
			setRandomDigits(1);
		}
		else if(correctAnswer<100){
			corrAnswDigits[0] = correctAnswer/10;
			corrAnswDigits[1] = correctAnswer % 10;
			setRandomDigits(2);
		}
		else{
			corrAnswDigits[0] = correctAnswer/100;
			corrAnswDigits[1] = (correctAnswer/10) % 10;
			corrAnswDigits[2] = correctAnswer % 100 - corrAnswDigits[1]*10;
			setRandomDigits(3);
		}
		Collections.shuffle(yPlaces);
		
	}
	
	public float getCorAnswYPos(){
		return yPlaces.get(0);
	}
	
	void setRandomDigits(int k){ //k=number of digits
		wr[0] = (int)(Math.random()*9)+1;
		wr[1] = (int)(Math.random()*9)+1;
		wr[2] = (int)(Math.random()*9)+1;
		if(k==1){
			for(int i=0; i<3; i++){
				while(wr[i] == correctAnswer || wr[i] == wr[(i+1)%3] || wr[i] == wr[(i+2)%3]){
					wr[i] = (int)(Math.random()*9)+1;
				}	
			}
		}
		else{
			wr[3] = (int)(Math.random()*10);
			wr[4] = (int)(Math.random()*10);
			wr[5] = (int)(Math.random()*10);
			if(k==2){
				for(int i=0; i<3; i++){
					while(wr[i]*10+wr[i+3] == correctAnswer || wr[i]*10+wr[i+3] == wr[((i+1)%3)]*10+wr[((i+1)%3)+3] || wr[i]*10+wr[i+3] == wr[((i+2)%3)]*10+wr[((i+2)%3)+3]){
						wr[i] = (int)(Math.random()*10);
						wr[i+3] = (int)(Math.random()*10);
					}	
				}
			}
			else{	
				wr[6] = (int)(Math.random()*10);
				wr[7] = (int)(Math.random()*10);
				wr[8] = (int)(Math.random()*10);
				for(int i=0; i<3; i++){
					while(wr[i]*100+wr[i+3]*10+wr[i+6] == correctAnswer || wr[i]*100+wr[i+3]*10+wr[i+6] == wr[((i+1)%3)]*100+wr[((i+1)%3)+3]*10+wr[((i+1)%3)+6] || wr[i]*100+wr[i+3]*10+wr[i+6] == wr[((i+2)%3)]*100+wr[((i+2)%3)+3]*10+wr[((i+2)%3)+6]){
						wr[i] = (int)(Math.random()*10);
						wr[i+3] = (int)(Math.random()*10);
						wr[i+6] = (int)(Math.random()*10);
					}	
				}
			}
		}
	}
}
